<?php
// +----------------------------------------------------------------------
// | Name : 合作项目报名
// +----------------------------------------------------------------------
// | Author: zbh <971361262@qq.com>
// +----------------------------------------------------------------------
// | date: 2017-12-22
// +----------------------------------------------------------------------
namespace Admin\Model;

use Think\Model;
use Tool\Qiniu\Auth;

class UploadQiNiuModel extends Model {

    //上传文件到七牛
    public function uploadQiNiu($filePath,$key){
        //import('Qiniu.functions');
        import('Tool.Qiniu.functions');

        // 用于签名的公钥和私钥
        $accessKey = '**';
        $secretKey = '**';

        // 初始化签权对象
//        $auth = new Auth($accessKey, $secretKey);

        // 空间名  https://developer.qiniu.io/kodo/manual/concepts
        $bucket = 'bucket-name';
        // 生成上传Token
//        $token = $auth->uploadToken($bucket);

        $file_contents = json_decode(file_get_contents('http://120.78.93.29:8012/qiNiu/upToken'),true);

        $token=$file_contents['data'];

//        var_dump($token) ;exit;
        // 构建 UploadManager 对象
        $uploadMgr = new \Tool\Qiniu\Storage\UploadManager();
        // 上传文件到七牛

        return $uploadMgr->putFile($token, $key, $filePath);
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);


//        if ($err !== null) {
////            print_r('$err='.$err);
//            return false;
//        } else {
////            var_dump('$ret='.$ret);
//            return $ret;
//        }
    }

    //获取上传到七牛的下载地址(私有空间)

    function getQiNiuDownloadUrl($filename){
        $accessKey ="NRTnpYIaf3SGgO74m77ZdbFq-7CztyhCelsC91yC";
        $secretKey = "jjFVk_BL-uiIwOuSXZikV4QpAQNGl-UfbZ5m8LCG";
        // 构建Auth对象
        $auth = new \Tool\Qiniu\Auth($accessKey, $secretKey);
        // 私有空间中的外链 http://<domain>/<file_key>
        $baseUrl = 'http://or83ht4g2.bkt.clouddn.com/'.$filename;
        // 对链接进行签名
        $signedUrl = $auth->privateDownloadUrl($baseUrl);
        return $signedUrl;

    }




    //获取上传到七牛的下载地址(公开空间)

    function getQiNiuDownloadUrlOpen($filename){
        $accessKey ="NRTnpYIaf3SGgO74m77ZdbFq-7CztyhCelsC91yC";
        $secretKey = "jjFVk_BL-uiIwOuSXZikV4QpAQNGl-UfbZ5m8LCG";
        // 构建Auth对象
        $auth = new \Tool\Qiniu\Auth($accessKey, $secretKey);
        // 私有空间中的外链 http://<domain>/<file_key>
        $baseUrl = 'http://papj6bhgm.bkt.clouddn.com/'.$filename;
        // 对链接进行签名
//        $signedUrl = $auth->privateDownloadUrl($baseUrl);
        return $baseUrl;

    }






}